<?php

namespace app\common\library;
use GuzzleHttp\Client;

/**
 * 快递类
 */
class Express
{
    //快递100授权码
    public $key = 'XJrunckI1942';
    //快递100查询公司编号
    public $customer = 'E6123E969BEC9872DA7F39CF854F9F07';
    public $params;
    public $url = 'https://poll.kuaidi100.com/poll/query.do';
    public function __construct($data)
    {
        $array = [
            'com' => $data['express_com'],             // 快递公司编码
            'num' =>$data['express_no'],    // 快递单号
            'resultv2' => 1
        ];
        $json = json_encode($array);
        $this->params = [
            'customer' => $this->customer,
            'param' => $json,
            'sign' => strtoupper(md5($json . $this->key . $this->customer))
        ];
  
    }
    /**
     * Json方式 快递100查询订单物流轨迹
     * @param string $shipper
     * @param string $code
     * @return int|string
     */
    public function getOrderTracesByJson()
    {
        $result = new client();
        // $result = $result->post($this->url, $this->params);
        //根据公司业务处理返回的信息......
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_URL, $this->url);
        curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($this->params));
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        $result = curl_exec($ch);
        // 第二个参数为true，表示格式化输出json
        $data = json_decode($result,true);
        return $data;
    }
}
